
#pragma once

#include "ITask.h"
#include "LuaEng.h"
#include <string>


//! A task which periodically calls a Lua function.
//!
class TaskPeriodicalCall : public ITask
{

	std::string mFunc;
	std::string mFile;
	lua_State * mL;
	bool mReload;

public:

	//! Constructs the object.
	//!
	//! @param L Pointer to the lua_State through which to make the call.
	//! @param func Name of the function to call.
	//! @param file (Optional) File in which to find the function. If an empty
	//! string is passed, it assumes that the function has already been loadead
	//! into the Lua state.
	//! @param reload (Optional) If set to true and if a file has been specified, that
	//! file will be reloaded after each call.
	TaskPeriodicalCall(lua_State * L, std::string const & func, 
		std::string const & file = "", bool reload = false)
		: mFunc(func), mL(L), mFile(file)
	{
		mReload = reload;
		if(file.empty())
			mReload = false;
		else
			LuaEng::RunFile(mL, mFile);
	}
	

	//! Executes the call to the specified function.
	//!
	virtual bool Execute()
	{
		if(mReload)
			LuaEng::RunFile(mL, mFile);
		LuaEng::EmpezarLlamada(mL, mFunc);
		LuaEng::TerminarLlamada(mL, 0);
		return true;
	}

};